package com.example.whowantstobemillionaireandroid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnGenericMotionListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {


    	
    	

    
    	
    	
    	
    	
    	

    	
    	
    	

    
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        
        
        
    	final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Please Enter Your Email Address");
    	alert.setMessage("Message");
    	final EditText input = new EditText(this);
    	alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    	public void onClick(DialogInterface dialog, int whichButton) {
    	  Editable value = input.getText();
    	  // Do something with value!
    	  }
    	});
    	alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	  public void onClick(DialogInterface dialog, int whichButton) {
    	    // Canceled.
    	  }
    	});
     
        
        
        
    	
    	TextView textView2 = (TextView) findViewById(R.id.textView2);
    	textView2.setOnClickListener(new View.OnClickListener()
            {

                public void onClick(View v)
                    {
                		alert.show();
                    
                    }
            });
        



        
        
        
        
        
        
        
        
        
        
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	EditText mEditUser   = (EditText)findViewById(R.id.editText1);
            	EditText mEditPassword   = (EditText)findViewById(R.id.editText2);
            	postData(mEditUser.getText().toString(),mEditPassword.getText().toString());
            }
        });
    }
    private  class MultiThreadingClass extends AsyncTask<String, Integer, Void> {
		protected Void doInBackground(String... params) {
		    HttpClient httpclient = new DefaultHttpClient();
		    HttpPost httppost = new HttpPost("http://10.0.0.4/CodeigniterPart/CodeIgniter_2.1.3/index.php/main");
		    try {
		        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
				JSONObject object = new JSONObject();
				try 
				{
					object.put("username", params[0]);
					object.put("password", params[1]);
					object.put("tag", "login");
				} 
				catch (JSONException e) 
				{
				    e.printStackTrace();
				}
				System.out.println();
		        nameValuePairs.add(new BasicNameValuePair("jsonString", object.toString()));
		        
		        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
		    } catch (ClientProtocolException e) {
		        // TODO Auto-generated catch block
		    } catch (IOException e) {
		        // TODO Auto-generated catch block
		    }
			return null;
		}
    }


    public void postData(String aUserName,String aPassword) {
 
    	new MultiThreadingClass().execute(aUserName,aPassword);

    } 

}


